//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
* 导航插件类；
*/



define(function (require, exports, module) {
	var CNavigationPanel_MainFrmEventHandler = require('./WapNPMainFrmEventHandler.js');
	FXREADER.WapNavigationPanelPluginName = "WapNavigationPanelPlugin";
    FXREADER.CWapNavigationPanelPlugin = function (pReaderApp) {
        //子窗口列表；需要实现INavigationChildPanel接口;
        var _ChildPanelList = [],
        //当前处于显示状态的Panel;
              _CurVisableChildPanel = null,
              _pReaderApp = pReaderApp,
        _self = this,
        //按钮面板ID
        _tabPanelId = "tabPanel",
        //存放按钮的ul列表;
              _ButtonListID = "btnList",
        //存放子面板内容;
              _TreePanelID = "fxtree_panel",
        //标题面板；
              _titlePanelId = "biaotou",
        //标题ID;
              _titleID = "panelTitle",
        //隐藏面板的按钮ID;
              _backbtId = "lefthid",
        //右侧面板是否处于显示状态;
               _bContentVisable = false;
        //创建面板的div内容；
        var _panelSize =
        {
            nTreePanelWidth: 0,
            nTreePanelHeight: 0
        };
        function _CreateHtmlContent() {
        	  //左侧的按钮面板；
            var html = "<div id='" + _tabPanelId + "' class='fwr_navigation_bar_tabmenu_wap' style='width:100%;height:100%;position:absolute;left:0px;top:0px;z-index:9999'>";
            	//+"<ul id='" + _ButtonListID + "'>";
         //"</ul></div>";
            
            
            


            //右侧面板；
            html += "<div id='" + _TreePanelID + "' class='fwr_left_panel_content' style='position:absolute;overflow: hidden; width: 100%; height: 100%;  color:#456176;'>";
            //右侧标题面板;
            html += "<div id='"+_titlePanelId+"' class='fwr_ribbon_topbar1_wap'>" +
            			"<div id='"+_backbtId+ "'class='fwr_toolbar_btn fwr_navigation_btn_frame_wap_right'>"+
            				"<a class='fwr_navigation_btn_wap_right'/>" +
            			"</div>" +
            			"<ul id='" + _ButtonListID + "'>" +
            			"</ul>" +
            		"</div>";

            /*html += "<div style='padding:10px 0px 0px 10px'><div style='float:left;' id='" + _titleID + "'></div><div id='" + _backbtId
                            + "' style='float:right;' class='fwr_hide_panel_btn'></div></div>";*/
            //end 右侧标题面板;
            html += "</div>";
            //end 右侧面板;
            html += "</div>";

            $("#" + _pReaderApp.GetMainView().GetMainFrameID()).append(html);
        }



        function _BindEvents() {
            $("#" + _backbtId).bind("click",
				function () 
				{
					_self.Hide();
				}
			);
           /* $("#" + _popleftId).bind("click", function () {
                if(!_self.IsVisablePanel())
                {	
                	if(_CurVisableChildPanel == null)
                		_self.ShowChildPanel(_ChildPanelList[0]);
            		_self.DisablePanel(false); 
                }
                _self.OnSize(); return false;
            });*/

            var iCnt = _ChildPanelList.length;
            for (var i = 0; i < iCnt; i++) {
                var panel = _ChildPanelList[i];
                $("#" + panel.GetTabBtnID()).bind('click', { panel: panel }, function (event) {
                    _self.ShowChildPanel(event.data.panel); return true;
                });
            }
        }

		this.Show = function()
		{
			$("#"+_tabPanelId).removeClass("fwr_hidden");
			$("#"+_tabPanelId).show();
			 if(!_self.IsVisablePanel())
             {	
                	if(_CurVisableChildPanel == null)
                		_self.ShowChildPanel(_ChildPanelList[0]);
            		_self.DisablePanel(false); 
             }
             _self.OnSize(); return false;
		};

		this.Hide = function()
		{
			$("#"+_tabPanelId).addClass("fwr_hidden");
			$("#"+_tabPanelId).hide();
		};

        //public methods

        this.GetSplitterDivID = function () {
            return _TreeSplitterID;
        };

        this.GetMoveSplitterDivID = function () {
            return _TreeMoveSplitterID;
        };
        this.GetTreePanelDivID = function () {
            return _TreePanelID;
        };

        this.GetTabPanelDivID = function () {
            return _tabPanelId;
        };

        this.GetPanelWidth = function () {
          /*  var jqTabPanel = $("#" + _tabPanelId);
            var jqTreePanel = $("#" + _TreePanelID);
            var jqTreeSplitter = $("#" + _TreeSplitterID);
            var nTabPanelWidth = jqTabPanel.is(":hidden") ? 0 : jqTabPanel.width();
            var nTreePanelHeight = jqTreePanel.is(":hidden") ? 0 : jqTreePanel.width();
            var nTreeSplitterWidth = jqTreeSplitter.is(":hidden") ? 0 : jqTreeSplitter.innerWidth();
            return nTabPanelWidth + nTreePanelHeight + nTreeSplitterWidth;*/
        };


        //添加子窗口；
        this.AddChildPanel = function (pChildPanel) {
            if (pChildPanel == null)
                return;
            _ChildPanelList.push(pChildPanel);
        };

        this.IsVisablePanel = function () { return _bContentVisable; };

        this.DisablePanel = function (bDis) {
            /*if (bDis) {
                $("#" + _TreePanelID).addClass("fwr_hidden");
                $("#" + _popleftId).removeClass("fwr_hidden");
				if (_CurVisableChildPanel != null) {
					$("#" + _CurVisableChildPanel.GetPanelID()).addClass("fwr_hidden");
					$("#" + _CurVisableChildPanel.GetTabBtnID() + ">img").attr("src", _CurVisableChildPanel.GetTabNorImgUrl());
				}
            }
            else {
                $("#" + _TreePanelID).removeClass("fwr_hidden");
                $("#" + _TreeSplitterID).removeClass("fwr_hidden");
                $("#" + _popleftId).addClass("fwr_hidden");
            }*/
            _bContentVisable=!bDis;
        };

        //显示某个子panel;
        this.ShowChildPanel = function (pChildPanel2Show) {
            if (!_bContentVisable) {
                _self.DisablePanel(false); _self.OnSize();
            }

            if (pChildPanel2Show == null)
                return;
            if (_CurVisableChildPanel == pChildPanel2Show)
                return;
            if (_CurVisableChildPanel != null) {
                $("#" + _CurVisableChildPanel.GetPanelID()).addClass("fwr_hidden");
                $("#" + _CurVisableChildPanel.GetTabBtnID()).removeClass("active");
            }

            var jqPanel = $("#" + pChildPanel2Show.GetPanelID());
            if (jqPanel.hasClass("fwr_hidden")) {
                jqPanel.removeClass("fwr_hidden");
                pChildPanel2Show.OnSize(_panelSize.nTreePanelWidth, _panelSize.nTreePanelHeight);
                $("#" + _titleID).text(pChildPanel2Show.GetTitle());
                $("#" + pChildPanel2Show.GetTabBtnID()).addClass("active");
            }
            _CurVisableChildPanel = pChildPanel2Show;
        };


        this.OnSize = function () {
            var jqLeftPanel = $("#" + _pReaderApp.GetMainView().GetLeftPanelID());
            jqLeftPanel.width(_self.GetPanelWidth());
            _pReaderApp.OnResize();
        };

        this.OnMainFrmSize = function () {
            if (!_bContentVisable)
                return;
            var jqTreePanel = $("#" + _TreePanelID);
            var titlePanelHeight = $("#" + _titlePanelId).height();
            _panelSize.nTreePanelWidth = jqTreePanel.width();
            _panelSize.nTreePanelHeight = jqTreePanel.height()-titlePanelHeight;
            if (_CurVisableChildPanel != null)
                _CurVisableChildPanel.OnSize(jqTreePanel.width(), jqTreePanel.height()-titlePanelHeight);
        };

        //获取Plugin名称;
        this.GetPluginName = function () {
            return FXREADER.WapNavigationPanelPluginName;
        };
        //插件被注册的时候调用;
        this.PIOnRegister = function () {
        };
        //模块初始化;
        this.PIInit = function () {
        	  _CreateHtmlContent();
              var iCnt = _ChildPanelList.length;
              var jqBtnList = $("#" + _ButtonListID);
              var jqTreePanel = $("#" + _TreePanelID);

              for (var i = 0; i < iCnt; i++) {
                  var panel = _ChildPanelList[i];
                  if (i==iCnt - 1 && iCnt>1) {
                      jqBtnList.append("<li class='fwr_menu_margin_wap fwr_right_border_radius' id='" + panel.GetTabBtnID() + "' style='cursor:pointer;' title='" +
                          panel.GetTitle() + "'><span>"+panel.GetTitle()+"</span>");
                  }
                  else {
                	  if(i==0)
                	  {
                		  if(iCnt==1)
                		  {
                			  jqBtnList.append("<li class='fwr_border_radius' id='" + panel.GetTabBtnID() + "' style='cursor:pointer;' title='" +
                					  panel.GetTitle() + "'><span>"+panel.GetTitle()+"</span></li>");
                		  }
                		  else
                		  {
                			  jqBtnList.append("<li class='fwr_left_border_radius' id='" + panel.GetTabBtnID() + "' style='cursor:pointer;' title='" +
                					  panel.GetTitle() + "'><span>"+panel.GetTitle()+"</span></li>");
                		  }
                		  
                	  }
                	  else 
                		  jqBtnList.append("<li class='' id='" + panel.GetTabBtnID() + "' style='cursor:pointer;' title='" +
                				  panel.GetTitle() + "'><span>"+panel.GetTitle()+"</span></li>");
                  }
                  jqTreePanel.append("<div  id='" + panel.GetPanelID() + "' class='fwr_tab_content_wap fwr_hidden jScrollPaneContainer jScrollPaneScrollable '></div>");
              }

              if (_bContentVisable) {
				 
                  _self.DisablePanel(false);
              }
              else {
				   _self.Hide();
                  _self.DisablePanel(true);
              }

              _BindEvents();
             var pMainFrmEvnetHandler = new CNavigationPanel_MainFrmEventHandler(_self);
             _pReaderApp.RegisterMainFrmEventHandler(pMainFrmEvnetHandler);
              _self.OnSize();
              if (iCnt > 0 && _bContentVisable)
                  _self.ShowChildPanel(_ChildPanelList[0]);
              _pReaderApp.StepProgress();
        };

        //模块卸载;
        this.PIUnload = function () {
        };

        this.GetReaderApp = function () {
            return _pReaderApp;
        };
    };



    exports.Reader_CreatePlugin = function (pReaderApp) {
        var pNavigationPanelPlugin = new FXREADER.CWapNavigationPanelPlugin(pReaderApp);
        return pNavigationPanelPlugin;
    };
});